package com.new2i.service.impl;

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.new2i.dao.able.DoctorDao;
import com.new2i.dao.able.ScreenDoctorDao;
import com.new2i.model.DoctorEntity;
import com.new2i.model.ScreenDoctorEntity;
import com.new2i.service.able.PassportService;
import com.new2i.util.EncryptionUtil;
import com.new2i.util.RegexUtil;

/** 
 * 通行证业务逻辑处理实现类
 * 
 * @since 2015/09/04
 * @author 2i
 *
 */
@Service("PassportService")
public class PassportServiceImpl implements PassportService{

	@Autowired
	private DoctorDao mDoctorDao; 
	
	@Autowired
	private ScreenDoctorDao mScreenDoctorDao;
	 
	private static Logger logger = Logger.getLogger(PassportService.class);
	
	@Override
	public HashMap<String, Object> doctorLogin(DoctorEntity doctor) { 
		HashMap<String, Object> rHashMap = new HashMap<String, Object>();
		String nickname = doctor.getNickname();
		String password = doctor.getPassword();
		if(nickname == null || nickname.length() <= 0){ 
			rHashMap.put("code", 310);
			rHashMap.put("err_msg","用户名不能为空");
			return rHashMap;
		}
		if(password == null || password.length() <= 0){ 
			rHashMap.put("code", 310);
			rHashMap.put("err_msg","密码不能为空");
			return rHashMap;
		}   
		if(!RegexUtil.validateEncodeMD5(password)) { //密码格式不正确
			rHashMap.put("code", 311);
			rHashMap.put("err_msg","密码格式不正确");
			return rHashMap;
		}
		int doctor_id = -1;
		String tel = null; 
		
		if(RegexUtil.validateDoctorId(nickname)) {//医生id登录
			doctor_id = Integer.parseInt(nickname);
			nickname = null; 
		}else if(RegexUtil.validatePhoneNum(nickname)) {//电话号码登录
			tel = nickname;
			nickname = null;
		}//别名登录 
		HashMap<String, Object> sDoctorInfo = mDoctorDao.getDoctorDescByITN(doctor_id, tel, nickname);
		if(sDoctorInfo == null || sDoctorInfo.isEmpty()) { //无此用户
			rHashMap.put("code", 322);
			rHashMap.put("err_msg","用户不存在,请检查用户名");
			return rHashMap;
		}
		String pwdStr = sDoctorInfo.get("password").toString();
		logger.debug(pwdStr);
		//获取客户端传来的密码md5
		String objPwdStr = EncryptionUtil.decodeMD5Desc2Simp(password);
		if(!objPwdStr.equals(pwdStr)) {
			rHashMap.put("code", 323);
			rHashMap.put("err_msg","密码错误");
			return rHashMap;
		} 
		sDoctorInfo.remove("password");
		return sDoctorInfo;
	}
	

	@Override
	public HashMap<String, Object> screenDoctorLogin(ScreenDoctorEntity scDoctor) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>();
		String tel = scDoctor.getTel();  
		String password = scDoctor.getPassword();   
		if(tel == null) {
			rHashMap.put("code", 312);
			rHashMap.put("err_msg", "请输入登录的电话号码"); 
			return rHashMap;
		}else if(!RegexUtil.validatePhoneNum(tel)){
			rHashMap.put("code", 313);
			rHashMap.put("err_msg", "请输入合法的电话号码"); 
			return rHashMap; 
		} 
		if(password == null) {
			rHashMap.put("code", 312);
			rHashMap.put("err_msg", "请输入密码"); 
			return rHashMap;  
		} 
		try { 
			HashMap<String, Object> sHashMap = mScreenDoctorDao.getScreenDoctorByTel(tel); 
			if(sHashMap == null || sHashMap.isEmpty()) { 
				rHashMap.put("code", 313);
				rHashMap.put("err_msg", "无此账户"); 
				return rHashMap; 
			} 
			String objPwd = (String) sHashMap.get("password");
			if(password.equals(objPwd)) {
				sHashMap.remove("password"); 
				rHashMap.putAll(sHashMap);
				return rHashMap;  
			}else { 
				rHashMap.put("code", 314);
				rHashMap.put("err_msg", "密码错误");  
			}
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "登录失败"); 
		} 
		return rHashMap;
	}


}
